<template>
  <div class="container" v-title data-title="洛阳单套餐活动">
    <div class="suite" v-if="state">
      <div class="thumbImg">
        <img :src="require('./styles/asset/luoyang/ly_001.png')" />
      </div>
      <ul class="coupon">
        <li class="active clearfix">
          <div class="left"><p class="time">包月套餐（30小时）</p><p class="hints">一个月内可累计使用，相当于每小时低至9.9元~</p></div>
          <div class="right">￥<span>{{price[0]}}</span></div>
        </li>
      </ul>
      <div class="thumbImg" style="margin-bottom:1.4rem;">
        <img :src="require('./styles/asset/luoyang/ly_002.png')" />
      </div>

    </div>

    <div class="suite"  v-else>

    </div>
    <div class="btn clearfix">
        <p class="hint">套餐有效期：30天</p>
        <div class="b-left">{{price[0]}}.00 </div>
        <div class="b-right" @click="buySuite(productId)">立即购买</div>
    </div>
    <simplert ref="simplert"></simplert>
  </div>
</template>

<script>
var wx = require('weixin-js-sdk');
  import Simplert from '../../components/dialog/dialog.vue';
  let filters = {
    parseURL:function(url) {
      let a = document.createElement('a');
      a.href = url;
      return {
        params: (function () {
          let ret = {},
            param = a.href.split('?')[1],
            seg = param.split('&'),
            len = seg.length,
            i = 0,
            s;
          for (; i < len; i++) {
            if (!seg[i]) {
              continue;
            }
            s = seg[i].split('=');
            ret[s[0]] = s[1];
          }
          return ret;
        })()
      }
    }
  };

  export default {
    data () {
      return {
        productId:'',
        price: [299],
        token:"",
        state:true,
        postState:false,
        img: require('../styles/asset/share.jpg'),
        id:'',
        wx_app:''
      }
    },
    created () {

    },
    mounted(){
      const that = this;
      const url = window.location.href;
      this.id = filters.parseURL(url).params.id;
      if(url.indexOf('wx_app')>0){
        this.wx_app = filters.parseURL(url).params.wx_app;
        this.detail = filters.parseURL(url).params.detail;
        if(this.detail == 'true'){
          this.orderId = filters.parseURL(url).params.orderId;
        }
      }else{
        this.wx_app = ''
      }
      this.$util.getUserToken().then(token=>{
        that.token = token;
        that.getSuiteId();
      })
      let root = process.env.MAIN_ROOT;
      this.$util.sendShareMessage("有驾照的统统看过来，共享汽车，快来试试>>","快来开共享汽车！用套餐，开车更划算！30小时超值套餐活动火热进行中......", root+this.img,url);
      if(url.indexOf('wx_app')>0){
        var wx_url = url.substr(29),
            seg = wx_url.split('&'),
            newUrl = '',
            s;
        for (var i=0; i < seg.length; i++) {
          s = seg[i].split('=');
          console.log('web'+[i]+','+s)
          if(s[0] =='share'|| s[0]=='token'){
            if(s[0]=='share'){

            }else{
                newUrl+='&token=';
            }
          }else{
            if(i== seg.length-2){
              newUrl += seg[i];
            }else{
              newUrl += seg[i]+'&';
            }
            console.log(newUrl);
          }

        }
        // 小程序分享title，url
        wx.miniProgram.postMessage({
          data: {
            title: '30小时超值套餐',
            wx_url: newUrl
          }
        })
      }
    },
    components: {
      Simplert
    },
    methods: {
      open (title, confirmBtn,customConfirmBtnText,onConfirm,type,onClose,message) {
        let obj = {
          title: title,
          useConfirmBtn: confirmBtn,
          customConfirmBtnText: customConfirmBtnText,
          onConfirm:onConfirm,
          type: type,
          onClose:onClose,
          message:message
        }
        this.$refs.simplert.openSimplert(obj)
      },
      getSuiteId(){
        let that = this;
        that.$api.post('/activity/v1/authApi/getIdsByActivity?id=' + this.id,{},s=>{
          s.module.forEach(function(e){
            that.productId = e.id
          })
        },f=>{
          that.open('套餐信息获取失败',false,'','','fade_alert',function(){

          });
        },{
          'requestPlatform': that.wx_app,
          'token': that.token
        })
      },
      buySuite(pid){
        if(!this.wx_app){
            let userAgentObj = navigator.userAgent;
            if(userAgentObj.match(/MicroMessenger/i)){
              //$(".weiChat").show();
              if(userAgentObj.match(/Android/i)){
                window.location.href = 'http://app.qq.com/#id=detail&appid=1106220919'
              }else if(userAgentObj.match(/iPhone/i)) {
                window.setTimeout(function(){
                  window.location.href = "https://itunes.apple.com/cn/app/%E7%94%B5%E6%96%91%E9%A9%AC-%E8%AE%A9%E5%87%BA%E8%A1%8C%E6%9B%B4%E5%AE%89%E5%85%A8/id1252928655?mt=8";
                },200)
              }else{
                window.location.href="http://download.xingjiakeji.com/";
              }
              return;
            }else{
              if(!this.token || this.token == undefined || this.token == ""){
                window.setTimeout(function(){
                  if(userAgentObj.match(/Android/i)){
                    window.location.href = 'http://app.qq.com/#id=detail&appid=1106220919';
                  }else if(userAgentObj.match(/iPhone/i)) {
                    window.location.href = "https://itunes.apple.com/cn/app/%E7%94%B5%E6%96%91%E9%A9%AC-%E8%AE%A9%E5%87%BA%E8%A1%8C%E6%9B%B4%E5%AE%89%E5%85%A8/id1252928655?mt=8";
                  }
                },1000);
                return;
              }
            }
        }
        if(this.postState){
          return
        }else{
          this.postState = true;
        }
        const that = this;
        this.open('您是否确认购买此套餐',true,'确认',function(){
          that.$api.post('/product/v1/authApi/buy?productId=' + pid,{},s=>{
            that.postState = false;
            let userAgentObj = navigator.userAgent;
            that.open('购买成功',false,'','','fade_success',function(){
              //兼容小程序
              if(that.wx_app){
                  if(that.detail == 'true'){
                    wx.miniProgram.reLaunch({url: '../detail/detail?orderId=' + that.orderId})
                  }
              }else{
                if(userAgentObj.match(/Android/i)){
                  window.control.refreshWallet();
                }else if(userAgentObj.match(/iPhone/i)){
                  refreshWallet();
                }
                that.$router.go(-1)
              }
            });
          },f=>{
            that.postState = false;
            if(f.resultCode == '30006')
            {
              that.open('余额不足请先充值！',true,'充值',function(){
                let userAgentObj = navigator.userAgent;
                //兼容小程序
                if(that.wx_app){
                  wx.miniProgram.navigateTo({url: '../user/balance/balance?detail=true'})
                }else{
                  if(userAgentObj.match(/Android/i)){
                    window.control.recharge();
                  }else if(userAgentObj.match(/iPhone/i)){
                    recharge();
                  }
                }
              })
            }else if(f.errorMessage){
              that.open(f.errorMessage,false,'','','fade_alert',function(){});
            }else{
              that.open('购买失败',false,'','','fade_alert',function(){});
            }
          },{
            'requestPlatform': that.wx_app,
            'token': that.token
          })
        },'',function(){
          that.postState = false;
        },"")
      },
    }
  }
</script>
<style scoped >
  @import "./styles/styles.css";
  .container{  width: 100%; height: 100%;}
  .suite{  margin: 0 auto; max-width: 750px; height: 100%; overflow-y: scroll;  background-color: #f7ece9;}
  .thumbImg>img{ width: 100%;}
  .thumb{ padding-bottom:0.2rem;}
  .thumb>img{ width: 100%;}
  .coupon{
    width:7rem;
    margin:-0.9rem auto 0 auto;
  }
  .coupon li{
  height:1.62rem;
  padding-left: 0.38rem;
  line-height: 1.44rem;
  border-radius: 0.15rem;
  border:0.03rem solid transparent;
  background: #fff;
  color:#999;
  font-size: 0.33rem;
  margin-bottom:0.3rem;
  box-shadow: 0 2px 25px 0 #ff7e59;
  }
  .coupon .active{
    border-color: #ff7e59;
    /*background: #fff2ee;*/
  }

  .coupon li>div{
  float: left;
  }
  .coupon .left{
    width:4.5rem;
    margin-top:0.2rem;
    font-size: 0.33rem;
    color:#333;
    font-weight: bold;
  }
  .coupon .left p.time{
    line-height: 0.51rem;
  }
  .coupon .active .left{
    padding-right:0.5rem;
    border-right:1px dashed #ff5d2e;
    color:#ff5d2e;
  }
  .left p.hints{
    font-size: 0.20rem;
    line-height: 0.35rem;
    color:#8c8c8c;
    font-weight: 500;
  }
  .coupon .middle{
    float: right;
    text-align: center;
    text-decoration: line-through;
    width:1rem;
    font-size: 0.18rem;
    color:#999;
  }
  .coupon .right{
    float: right;
    width:1.38rem;
    margin-right:0.2rem;
    font-size: 0.33rem;
    color:#ff5d2e;
    font-weight: bold;
  }
   .coupon .right span{
    font-size: 0.51rem;
   }
  .coupon .red{
    color:#ff5d2e;
  }
  .btn{
    position: fixed;
    left:0;
    right:0;
    bottom:0;
    height:0.98rem;
    background: #fff;
    font-size: 0.3rem;
    color:#ff5d2e;
    border-top-right-radius: 0.25rem;
    border-top-left-radius: 0.25rem;
  }
  .btn .b-left{
    width:2.3rem;
    height:0.98rem;
    font-size:0.4rem;
    line-height: 0.98rem;
    padding-left:0.25rem;
    float: left;
  }
  .hint{
    position: absolute;
    left:1.6rem;
    top:0;
    font-size:0.26rem;
    line-height: 0.98rem;
    color:#666;

  }
  .btn .b-right{
    float: right;
    width:2.3rem;
    height:0.98rem;
    line-height: 0.98rem;
    background: #ff5d2e;
    color:#fff;
    text-align: center;
    border-top-right-radius: 0.25rem;
  }
</style>
